﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Windows.Threading;
namespace AssessmentTool.PresentationLogic
{
    public class EventDispatcher
    {
        public delegate void delegateEvent(EventArgs e);
        private static EventDispatcher _instance;
        
        public static EventDispatcher getInstance()
        {
            if (_instance == null)
            {
                _instance = new EventDispatcher();
            }

            return _instance;
        }
        private EventDispatcher()
        {
            sottoscrizioni = new Dictionary<string, List<delegateEvent>>();
        }
     
        Dictionary<String, List<delegateEvent>> sottoscrizioni;
        public void subscrive(String evento, delegateEvent d)
        {
            List<delegateEvent> list;
            if(!sottoscrizioni.ContainsKey(evento))
            {
                list = new List<delegateEvent>();
                sottoscrizioni.Add(evento,list);
                
            }
            else
            {
               
               list=sottoscrizioni[evento];
            }
            if(!list.Contains(d))
                list.Add(d);
        }

        public void dispatch(String evento,EventArgs e)
        {
            if (!sottoscrizioni.ContainsKey(evento))
                return;
            foreach(delegateEvent method in sottoscrizioni[evento])
            {
                method.DynamicInvoke(e);
            }
        }

        
    }
}
